//
//  SureDingdanViewController.m
//  Products_B
//
//  Created by lanou on 16/5/31.
//  Copyright © 2016年 朋学友. All rights reserved.
//

#import "SureDingdanViewController.h"
#import "AddressViewController.h"
#import "ChuanzhiModel.h"
#import "AddressListViewController.h"
#import "FMDBTools.h"
@interface SureDingdanViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (strong, nonatomic) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *chuanzhiModelArray;
@property (nonatomic, strong) NSMutableArray *tempModelArray;
@end

@implementation SureDingdanViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.tempModelArray = [NSMutableArray array];
    // 必须打开数据库
    [[FMDBTools shareInstance] openDB];

    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"确认订单";
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kWidth, kHeight - 64) style:UITableViewStylePlain];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.backgroundColor = [UIColor colorWithRed:245 / 255.0 green:245 / 255.0 blue:245 / 255.0 alpha:1];
    [self.view addSubview:_tableView];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 4;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    // 在这里，不用去缓存池找cell，因为以往去缓存池找cell都是为了重复的cell不用再去重复创建。而这里的每一个cell布局都不一致，都是独一无二的，所以每个cell都要allo和init
//    static NSString *ID = @"cell";
//    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
//    if (cell == nil) {
//        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
//    }
    
#warning 为什么cell的高度不对
    UITableViewCell *cell = [[UITableViewCell alloc] init];
    
    if (indexPath.section == 0) {
        UILabel *addressLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 60, 20)];
        addressLabel.text = @"收货地址";
        addressLabel.font = [UIFont systemFontOfSize:12];
        addressLabel.alpha = 0.8;
        [cell addSubview:addressLabel];
        
        _namePhoneLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 40, kWidth - 60, 20)];
         _chuanzhiModelArray = [[FMDBTools shareInstance] selectAllDataWthTableName:@"shouhuoAddress"];
        // 遍历从数据库中取出的数组
        for (ChuanzhiModel *yesModel in _chuanzhiModelArray) {
            if (yesModel.isMoren) {
                // 将标识为YES的mdoel重新组合成新的数组
                [_tempModelArray addObject:yesModel];
            }
        }
        // 取最后一个为YES的model(重新运行程序时，要将默认地址设置为最后一次为YES的那个地址)
        ChuanzhiModel *model = _tempModelArray.lastObject;
        if (model.nameString.length != 0 && model.phoneString.length != 0) {
            _namePhoneLabel.text = [NSString stringWithFormat:@"%@    %@",model.nameString,model.phoneString];
        }
        _namePhoneLabel.font = [UIFont systemFontOfSize:12];
        _namePhoneLabel.alpha = 0.6;
        [cell addSubview:_namePhoneLabel];
        
        _addressLabel2 = [[UILabel alloc] initWithFrame:CGRectMake(10, 70, kWidth - 30, 20)];
        if (model.addressString.length != 0) {
            _addressLabel2.text = model.addressString;
        }
        _addressLabel2.font = [UIFont systemFontOfSize:12];
        _addressLabel2.alpha = 0.6;
        [cell addSubview:_addressLabel2];
        return cell;
    }
    if (indexPath.section == 1) {
        UIImageView *picImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 640 * 80 / 360, 80)];
        [picImageView sd_setImageWithURL:[NSURL URLWithString:_imageUrlString]];
        [cell addSubview:picImageView];
        
        UILabel *priceLabel = [[UILabel alloc] initWithFrame:CGRectMake(kWidth - 150, 25, 60, 20)];
        priceLabel.text = [NSString stringWithFormat:@"%@元",_price];
        priceLabel.font = [UIFont systemFontOfSize:12];
        priceLabel.textColor = [UIColor orangeColor];
        priceLabel.alpha = 0.6;
        [cell addSubview:priceLabel];
        
        UILabel *numLabel = [[UILabel alloc] initWithFrame:CGRectMake(kWidth - 90, priceLabel.frame.origin.y, 60, 20)];
        numLabel.text = [NSString stringWithFormat:@"×%ld",_buyNumber];
        numLabel.font = [UIFont systemFontOfSize:12];
        numLabel.alpha = 0.5;
        [cell addSubview:numLabel];
        
        return cell;
    }
    if (indexPath.section == 2) {
        UILabel *postageLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 60, 20)];
        postageLabel.text = @"运费";
        postageLabel.font = [UIFont systemFontOfSize:12];
        postageLabel.alpha = 0.6;
        [cell addSubview:postageLabel];
        
        UILabel *postageLabel2 = [[UILabel alloc] initWithFrame:CGRectMake(kWidth - 90, postageLabel.frame.origin.y, 60, 20)];
        postageLabel2.text = _postAge;
        postageLabel2.font = [UIFont systemFontOfSize:12];
        postageLabel2.alpha = 0.6;
        [cell addSubview:postageLabel2];
        
        UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(10, 50, kWidth - 20, 1)];
        NSLog(@" // %f",cell.frame.size.height / 2);
        lineView.backgroundColor = [UIColor grayColor];
        lineView.alpha = 0.5;
        [cell addSubview:lineView];
        
        UILabel *RMBlabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 60, 60, 20)];
        RMBlabel.text = @"支付金额";
        RMBlabel.font = [UIFont systemFontOfSize:12];
        RMBlabel.alpha = 0.6;
        [cell addSubview:RMBlabel];
        
        UILabel *RMBlabel2 = [[UILabel alloc] initWithFrame:CGRectMake(kWidth - 90, RMBlabel.frame.origin.y, 60, 20)];
        NSInteger m = [_price integerValue];
        RMBlabel2.text = [NSString stringWithFormat:@"￥%ld",m * _buyNumber];
        RMBlabel2.font = [UIFont systemFontOfSize:12];
        RMBlabel2.alpha = 0.6;
        [cell addSubview:RMBlabel2];
        return cell;
    }
    
    if (indexPath.section == 3) {
        // 图片label组合
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 10;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 100;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        if (self.namePhoneLabel.text.length == 0) {
            AddressViewController *addressVC = [[AddressViewController alloc] init];
            [self.navigationController pushViewController:addressVC animated:YES];
            // block传值
            // 点击保存按钮时回调
            __block SureDingdanViewController *blockSelf = self;
            addressVC.block = ^(ChuanzhiModel *model) {
                blockSelf.namePhoneLabel.text = [NSString stringWithFormat:@"%@    %@",model.nameString,model.phoneString];
                blockSelf.addressLabel2.text = model.addressString;
            };
        } else {
            AddressListViewController *addressListVC = [[AddressListViewController alloc] init];
             _chuanzhiModelArray = [[FMDBTools shareInstance] selectAllDataWthTableName:@"shouhuoAddress"];
            addressListVC.chuanzhiModelArray = self.chuanzhiModelArray;
            [self.navigationController pushViewController:addressListVC animated:YES];
        }
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
